﻿using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;

namespace ZYing.LiquidThemeEngine.AspNetCore
{
    public class LiquidThemeViewResultExecutor : IActionResultExecutor<LiquidThemeViewResult>
    {
        public async Task ExecuteAsync(ActionContext context, LiquidThemeViewResult result)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));

            if (result == null) throw new ArgumentNullException(nameof(result));
            result.StatusCode ??= 200;

            var html = await LiquidThemeViewRender.RenderAsync(result.TemplateContext.Theme, result.View, result.TemplateContext);
            var response = context.HttpContext.Response;
            response.StatusCode = result.StatusCode.Value;
            await response.WriteAsync(html);
        }
    }
}